package com.systop.asset.service.impl;

import java.util.List;

import com.systop.asset.entity.Asset;
import com.systop.asset.entity.AssetAssignmentsDetail;
import com.systop.asset.entity.RepairDetail;
import com.systop.asset.entity.vo.AssetAssignmentVO;
import com.systop.asset.entity.vo.AssetFixVO;
import com.systop.asset.mapper.AssetAssignmentsDetailMapper;
import com.systop.asset.mapper.AssetMapper;
import com.systop.asset.mapper.RepairDetailMapper;
import com.systop.asset.utils.RandomCodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.systop.asset.mapper.RepairMapper;
import com.systop.asset.entity.Repair;
import com.systop.asset.service.IRepairService;

/**
 * 资产维修记录Service业务层处理
 * 
 * @author cc
 * @date 2025-07-04
 */
@Service
public class RepairServiceImpl implements IRepairService 
{
    @Autowired
    private RepairMapper repairMapper;
    @Autowired
    private RepairDetailMapper repairDetailMapper;
    @Autowired
    private AssetMapper assetMapper;

    /**
     * 查询资产维修记录
     * 
     * @param id 资产维修记录主键
     * @return 资产维修记录
     */
    @Override
    public Repair selectRepairById(Long id)
    {
        return repairMapper.selectRepairById(id);
    }

    /**
     * 查询资产维修记录列表
     * 
     * @param repair 资产维修记录
     * @return 资产维修记录
     */
    @Override
    public List<Repair> selectRepairList(Repair repair)
    {
        return repairMapper.selectRepairList(repair);
    }

    /**
     * 新增资产维修记录
     *
     * @param repair 资产维修记录
     * @return 结果
     */
    @Override
    public int insertRepair(Repair repair)
    {
        List<RepairDetail> assets = repair.getAssets();
        if(assets!=null){
            //TODO：设置维修单ID
            repair.setRepairNo(RandomCodeGenerator.generateFixCode());

            //新增资产维修记录
            repairMapper.insertRepair(repair);
            //新增资产维修明细表数据
            assets.forEach(assetment -> {
                assetment.setAssetsId(assetment.getId());
                assetment.setRepairId(repair.getId());
            });
            repairDetailMapper.insertRepairBatch(assets);


            // 批量修改资产状态
            //1.资产状态改为维修中
            //2.
            for(RepairDetail assetment:assets){
                Asset asset = new Asset();
                asset.setId(assetment.getAssetsId());
                asset.setStatus(3);
                //更改资产位置
                asset.setLocationId(assetment.getLocationId());

                assetMapper.updateAsset(asset);
            }
            return 1;
        } else {
            return 0;
        }

    }


    /**
     * 修改资产维修记录
     * 
     * @param repair 资产维修记录
     * @return 结果
     */
    @Override
    public int updateRepair(Repair repair)
    {
//        repair.setUpdateTime(DateUtils.getNowDate());
        return repairMapper.updateRepair(repair);
    }

    /**
     * 批量删除资产维修记录
     * 
     * @param ids 需要删除的资产维修记录主键
     * @return 结果
     */
    @Override
    public int deleteRepairByIds(Long[] ids)
    {
        return repairMapper.deleteRepairByIds(ids);
    }

    /**
     * 删除资产维修记录信息
     * 
     * @param id 资产维修记录主键
     * @return 结果
     */
    @Override
    public int deleteRepairById(Long id)
    {
        return repairMapper.deleteRepairById(id);
    }

    @Override
    public AssetFixVO selectDetail(String id){
        return repairMapper.selectDetail(id);
    }
}
